﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Collections;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class BaseService
    {
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="item">要添加的对象</param>
        /// <returns></returns>
        public bool Add(object item)
        {
            try
            {
                DBHelper.TransInsert(item);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }

        }
        /// <summary>
        /// 跟新某个实体类
        /// </summary>
        /// <param name="item">要更新的类</param>
        /// <returns></returns>
        public bool UpDate(object item)
        {
            try
            {
                DBHelper.TransUpdate(item);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 删除数据库中指定的实体对象
        /// </summary>
        /// <param name="item">删除的实体对象</param>
        /// <returns></returns>
        public bool Delete(object item)
        {
            try
            {
                DBHelper.TransDelete(item);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 根据实体对象名称和ID删除数据
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string Model, int id)
        {
            string hql = "delete from " + Model + " where Id =" + id;
            try
            {
                DBHelper.DeleteByHQL(hql);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 通过ID找到某个对象
        /// </summary>
        /// <typeparam name="T">要查询的类型</typeparam>
        /// <param name="t">类型</param>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public T GetItemById<T>(T t, int id)
        {
            try
            {
                return (T)DBHelper.GetObject(t.GetType(), id);
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return default(T);
            }
        }
        /// <summary>
        /// 返回某个类的所有数据
        /// </summary>
        /// <typeparam name="T">要查询的类型</typeparam>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public IList<T> GetItemList<T>(T t)
        {
            try
            {
                IQuery iq = DBHelper.ExecuteHQL("from " + t.GetType().ToString());

                if (iq != null)
                {
                    return iq.List<T>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return null;
            }

        }

        /// <summary>
        /// 用 hql  返回某个类的所有数据
        /// </summary>
        /// <typeparam name="T">要查询的类型</typeparam>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public IList<T> GetItemList<T>(String hql)
        {
            try
            {
                IQuery iq = DBHelper.ExecuteHQL(hql);

                if (iq != null)
                {
                    return iq.List<T>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return null;
            }

        }

        /// <summary>
        /// HQL 传参 查询 带分页 
        /// </summary>
        /// <param name="hql"></param>
        /// <param name="param"></param>
        /// <param name="first"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public IList<T> FindList<T>(String hql, Dictionary<string, object> param, int? first, int? end)
        {
            IQuery query = DBHelper.ExecuteHQL(hql);

            if (param != null && param.Count > 0)
            {
                foreach (String key in param.Keys)
                {
                    query.SetParameter(key.ToString(), param[key.Trim()]);
                }
            }
            if (first != null)
            {
                query.SetFirstResult(first.Value);
                if (end != null)
                {
                    query.SetMaxResults(end.Value);
                }
            }
            try
            {
                IList<T> objList = query.List<T>();
                return objList;
            }
            catch (Exception)
            {
                return null;
            }


        }


        public T GetItemByHQL<T>(string hql)
        {
            try
            {
                return (T)DBHelper.ExecuteHQL(hql).UniqueResult();
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return default(T);
            }
        }
        /// <summary>
        /// 返回一个结果
        /// </summary>
        /// <param name="hql"></param>
        /// <returns></returns>
        public object GetValue(string hql)
        {
            return DBHelper.ExecuteHQL(hql).UniqueResult();
        }

        /// <summary>
        /// 根据Id查找
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public object FindModelById(string model, int id)
        {
            string hql = "from " + model + " where Id=" + id;
            try
            {
                return DBHelper.ExecuteHQL(hql).UniqueResult();
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return default(object);
            }
        }
        /// <summary>
        /// 根据实体类批量删除数据
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool BatchDelete(string Model, string ids)
        {
            string hql = "delete from " + Model + " where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 批量审核数据
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="ids"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public bool BatchCheck(string Model, string ids, bool flag)
        {
            string hql = "update from " + Model + " set IsChecked = " + flag + " where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 省市区，查询根据code
        /// </summary>
        /// <param name="p"></param>
        /// <param name="proCode"></param>
        /// <returns></returns>
        public object  FindModelByCode(string model, string Code)
        {
            string hql = "from " + model + " where Code='" + Code+"'";
            try
            {
                return DBHelper.ExecuteHQL(hql).UniqueResult();
            }
            catch (Exception ex)
            {
                LogicUtil.AddALog(ex.ToString());
                return default(object);
            }
        }
    }
}
